package com.example.service.implService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.commons.utils.UniqueSixDigitGenerator;
import com.example.dao.EnrollInfoDao;
import com.example.dao.StuInfoDao;
import com.example.pojo.EnrollInfo;
import com.example.pojo.FamilyInfo;
import com.example.pojo.StuInfo;
import com.example.service.EnrollInfoService;
import com.example.service.FamilyInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service
public class EnrollInfoServiceImpl implements EnrollInfoService {
    @Resource
    private EnrollInfoDao enrollInfoDao;
    @Resource
    private FamilyInfoService familyInfoService;
    @Resource
    private StuInfoDao stuInfoDao;
    @Resource
    private UniqueSixDigitGenerator uniqueSixDigitGenerator;

    @Override
    public List<EnrollInfo> getAllEnroll() {
        return enrollInfoDao.getAllEnroll();
    }

    @Override
    public int addEnroll(EnrollInfo enrollInfo) {
        return enrollInfoDao.addEnroll(enrollInfo);
    }

    @Override
    public int batchDeleteEnrollInfo(List<Integer> ids) {
        return enrollInfoDao.batchDeleteEnrollInfo(ids);
    }

    @Override
    public String stuEnroll(StuInfo stuInfo, Integer enrollId) {
        FamilyInfo familyInfo = new FamilyInfo();
        // 生成绑定码
        stuInfo.setBindCode(uniqueSixDigitGenerator.generateUniqueSixDigitString());
        stuInfo.setAdmissionDate(LocalDate.now());
        // 新增学生信息
        String addResult = stuInfoDao.addOrUpdateStuInfo(stuInfo);
        if ("".equals(addResult)) {
            return null;
        }
        // 通过学生姓名获取到学生信息
        StuInfo stuByName = stuInfoDao.getStuByName(stuInfo.getName());
        //将幼儿id添加到家庭信息中
        familyInfo.setChildId(stuByName.getId());
        // 添加父亲或是母亲的姓名和电话号码
        familyInfo.setMemberName(stuInfo.getEmergencyContactName());
        familyInfo.setContactNumber(stuInfo.getEmergencyContactPhone());
        String i = familyInfoService.addOrUpdateFamilyInfo(familyInfo);
        if ("添加成功".equals(i)) {
            stuInfo.setId(stuByName.getId());
            if ("修改成功".equals(stuInfoDao.addOrUpdateStuInfo(stuInfo))) {
                return stuByName.getBindCode();
            }
        }
        return null;
    }

    @Override
    public IPage<EnrollInfo> getAllEnrollPage(int currentPage, int limit) {
        return enrollInfoDao.getAllEnrollPage(currentPage, limit);
    }

    @Override
    public IPage<EnrollInfo> getEnrollByTitle(int currentPage, int limit, String enrollTitle) {
        return enrollInfoDao.getEnrollByTitle(currentPage, limit, enrollTitle);
    }

    @Override
    public String addOrUpdateEnrollInfo(EnrollInfo enrollInfo) {
        return enrollInfoDao.addOrUpdateEnrollInfo(enrollInfo);
    }

    @Override
    public int deleteEnrollInfo(int eid) {
        return enrollInfoDao.deleteEnrollInfo(eid);
    }
}
